package com.tobias.gaokao.util;

import org.springframework.util.ObjectUtils;

public class FinanceUtils {

	public static String toHash(String key) {
		int arraySize = 11113;
		int hashCode = 0;
		for (int i = 0; i < key.length(); i++) {
			int letterValue = key.charAt(i) - 96;
			hashCode = ((hashCode << 5) + letterValue) % arraySize;
		}
		return "***" + hashCode;
	}

	public static String maskingWithoutLastFourDigits(String fieldValue) {
		if(!ObjectUtils.isEmpty(fieldValue) && fieldValue.length() > 4) {
			return fieldValue.replaceFirst(fieldValue.substring(0, fieldValue.length()-4), "****");
		} else {
			return "****" + generateRandomNumber(4 - fieldValue.length() ) + fieldValue;
		}
	}

	public static String generateRandomNumber(int n){
		return n <1 ? "" : Long.toString((long)(Math.random()*9*Math.pow(10,n-1)) + (long)Math.pow(10,n-1));
	}

}
